package service;

import java.util.ArrayList;
import java.util.List;


import bean.Role;
import bean.User;
import bean.model.PageModel;
import dao.UserDao;

public class StaffService {
	private UserDao userDao = new UserDao();
	
	public List<User> getStaffSearchList(String username, String sex, String lowerAge, String higherAge, String rigor) {
		List<User> list = new ArrayList();
		String timeFrom = "";
		String timeTo = "";	
		Boolean vague = false;
		java.text.Format formatter=new java.text.SimpleDateFormat("yyyy-MM-dd");    
		java.util.Date todayDate=new java.util.Date();   
		java.util.Date todayDate2=new java.util.Date(); 
		
		if(lowerAge.equals("")){
			timeTo = "";
		}else{
			long beforeTime = (todayDate.getTime()/1000)-60*60*24*365*(Integer.parseInt(lowerAge));
			todayDate.setTime(beforeTime*1000);  
			timeTo = formatter.format(todayDate);
		}
		
		if(higherAge.equals("")){
			timeFrom = "";
		}else{
			long beforeTime2 = (todayDate2.getTime()/1000)-60*60*24*365*(Integer.parseInt(higherAge));  		  
			todayDate2.setTime(beforeTime2*1000);
			timeFrom = formatter.format(todayDate2);
		}
		
		if(sex.equals("0")){
			sex = "";
		}else if(sex.equals("1")){
			sex = "男";
		}else{
			sex = "女";
		}	
		
		if(rigor.equals("true")){
			vague = true;
		}
		
		String[] arr1={"username","sex","timeFrom","timeTo"};
		String[] arr2={username, sex, timeFrom, timeTo};
		return list;
	}
	public void deleteStaff(String username){
		
		User user = userDao.getUser(username);
		userDao.deleteUser(user.getUserId());
		
		return;
		
	}
	
	public boolean addStaff(String username, String md5password,String birthday,String realName,String sex,int phoneNumber) {
		User user = new User();
		Role role = new Role();
		role.setRoleId(1);
		final int userNum = userDao.getUserNum()+1;
		user.setUserId(userNum);
		user.setUserName(username);
		user.setUserPassword(md5password);
		user.setBirthday(birthday);
		user.setPhoneNumber(phoneNumber);
		user.setSex(sex);
		user.setTruename(realName);
		user.setRole(role);
		
		return userDao.insertUser(user);
	}
	
    public int getStaffNum(){
		
		int num = userDao.getUserNum();
		
		return num;
		
	}
    
    public List<User> queryStaffByPage(final PageModel page) {
		
    	List<User> staffPage = userDao.queryByPage(page);
    	
		return staffPage;
	}
}
